#!/usr/bin/python

import sys
import gtk
import gtk.gdk
import gnomeapplet

class GnomeAppletSkeleton(gnomeapplet.Applet):
	"""Simple applet skeleton"""

	def __init__(self, applet, iid):
		"""Create applet"""
		self.applet = applet
#		init tooltip, HBox and EventBox
		self.__init_core_widgets()
#		init label
		self.init_additional_widgets()
#		init popup menu
		self.init_ppmenu()
#		connect events with handlers
		self.__connect_events()
#		connect handler of closing window
		self.applet.connect("destroy", self._cleanup)
#		do derived class sepcified initialization
		self.after_init()
#		show applet
		self.applet.show_all()

	def after_init(self):
		"""Special function to be overrided in derived classes"""
		pass

	def _cleanup(self,event):
		"""Finallization"""
		del self.applet

	def __init_core_widgets(self):
		"""Create internal widgets"""
#		init main container
		self.hbox = gtk.HBox()
#		init event box for handling events on label
		self.ev_box = gtk.EventBox()
#		enable tooltips on eventbox
		self.ev_box.set_has_tooltip(True)
#		add widgets to applet
		self.applet.add(self.hbox)
		self.hbox.add(self.ev_box)

	def init_additional_widgets(self):
		"""Create additional widgets"""
#		init label with text "Dummy"
		self.label = gtk.Label("Dummy")
#		add label to EventBox to be able to handle events on it
		self.ev_box.add(self.label)

	def init_ppmenu(self):
		"""Create popup menu"""
		self.applet.setup_menu("""
		<popup name="button3">
		<menuitem name="About Item" verb="About" stockid="gtk-about" />
		</popup>
		""", [("About", self.on_ppm_about),],None)

	def __connect_events(self):
		"""Connect applet's events to callbacks"""
#		connect event of clicking eventbox
		self.ev_box.connect("button-press-event", self.on_button)
#		connect event of placing mouse over eventbox
		self.ev_box.connect("query-tooltip", self.on_tooltip)
#		mouse button handlers (default: do nothing)
		self.button_actions = {
			1: lambda: None,
			2: lambda: None,
			3: lambda: None,
		}
		
	def on_button(self, widget, event):
		"""Action on pressing button in applet"""
#		call button-specific mouse click handler
		if event.type == gtk.gdk.BUTTON_PRESS:
			self.button_actions[event.button]()
	
	def on_tooltip(self, widget, x, y, keyboard_mode, tooltip):
		"""Action on entering"""
		info = "Hey, it just skeleton"
		tooltip.set_icon_from_icon_name("gnome-panel",self.applet.get_size())
		tooltip.set_text(info)
		return True		

	def on_ppm_about(self, event, data=None):
		"""Action on choosing 'about' in popup menu"""
#		show gnome standart about dialog
		about = gtk.AboutDialog()
#		set program name
		about.set_name("GNOME Applet Skeleton")
#		set program version
		about.set_version("0.1")
#		set copyrights
		about.set_copyright("Pythy <the.pythy@gmail.com>")
#		set program info
		about.set_comments("Simple skeleton for Python powered GNOME applet")
#		set license (GNU GPLv2)
		about.set_license("""
GNOME Applet Skeleton - Simple skeleton for Python powered GNOME applet
Copyright (C) 2010  Yurevich Yuri

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
		""")
#		set program website
		about.set_website("http://www.citforum.ru/programming/python/gnome_applets/index.shtml")
#		set program authors
		about.set_authors(["Pythy <the.pythy@gmail.com.>",])
#		set program logo (default logo of GNOME panel)
		about.set_logo_icon_name("gnome-panel")
#		run about dialog
		about.run()
#		correctly destroy object
		about.destroy()
		del about
